/// 通知消息模型 - 用于统一管理各种类型的通知
class NotificationMessage {
  final String id;
  final String type; // comment_reply, post_reply, like, follow, system
  final String title;
  final String content;
  final String? senderName;
  final String? senderId;
  final String? targetId; // 目标ID（帖子ID、评论ID等）
  final String? targetType; // 目标类型（post、comment等）
  final String? navigationPath; // 跳转路径（如：/community/19）
  final Map<String, dynamic>? navigationParams; // 跳转参数
  final Map<String, dynamic>? extraData; // 额外数据
  final DateTime timestamp;
  final bool isRead;
  final bool isOffline; // 是否为离线消息

  NotificationMessage({
    required this.id,
    required this.type,
    required this.title,
    required this.content,
    this.senderName,
    this.senderId,
    this.targetId,
    this.targetType,
    this.navigationPath,
    this.navigationParams,
    this.extraData,
    required this.timestamp,
    this.isRead = false,
    this.isOffline = false,
  });

  factory NotificationMessage.fromJson(Map<String, dynamic> json) {
    return NotificationMessage(
      id: json['id']?.toString() ?? '',
      type: json['type'] ?? 'system',
      title: json['title'] ?? '',
      content: json['content'] ?? '',
      senderName: json['senderName'],
      senderId: json['senderId']?.toString(),
      targetId: json['targetId']?.toString(),
      targetType: json['targetType'],
      navigationPath: json['navigationPath'],
      navigationParams: json['navigationParams'] as Map<String, dynamic>?,
      extraData: json['extraData'] as Map<String, dynamic>?,
      timestamp: json['timestamp'] != null 
          ? DateTime.parse(json['timestamp'])
          : DateTime.now(),
      isRead: json['isRead'] ?? false,
      isOffline: json['isOffline'] ?? false,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'type': type,
      'title': title,
      'content': content,
      'senderName': senderName,
      'senderId': senderId,
      'targetId': targetId,
      'targetType': targetType,
      'navigationPath': navigationPath,
      'navigationParams': navigationParams,
      'extraData': extraData,
      'timestamp': timestamp.toIso8601String(),
      'isRead': isRead,
      'isOffline': isOffline,
    };
  }

  /// 从WebSocket消息创建通知消息
  factory NotificationMessage.fromWebSocketMessage(Map<String, dynamic> wsContent) {
    final notificationType = wsContent['notificationType'] as String? ?? 'system';
    String title = '';
    String content = '';
    String? navigationPath;
    Map<String, dynamic>? navigationParams;
    
    switch (notificationType) {
      case 'comment_reply':
        title = '评论回复';
        content = '${wsContent['senderName'] ?? '有人'}回复了您的评论：${wsContent['replyContent'] ?? ''}';
        // 跳转到帖子详情页
        navigationPath = '/community/${wsContent['postId']}';
        navigationParams = {
          'postId': wsContent['postId'],
          'commentId': wsContent['commentId'],
          'scrollToComment': true,
        };
        break;
      case 'post_reply':
        title = '帖子回复';
        content = '${wsContent['senderName'] ?? '有人'}评论了您的帖子：${wsContent['replyContent'] ?? ''}';
        // 跳转到帖子详情页
        navigationPath = '/community/${wsContent['postId']}';
        navigationParams = {
          'postId': wsContent['postId'],
          'scrollToComments': true,
        };
        break;
      case 'like':
        title = '获得点赞';
        content = '${wsContent['senderName'] ?? '有人'}点赞了您的${wsContent['targetType'] == 'post' ? '帖子' : '评论'}';
        // 根据点赞类型决定跳转
        if (wsContent['targetType'] == 'post') {
          navigationPath = '/community/${wsContent['postId']}';
          navigationParams = {'postId': wsContent['postId']};
        } else {
          navigationPath = '/community/${wsContent['postId']}';
          navigationParams = {
            'postId': wsContent['postId'],
            'commentId': wsContent['commentId'],
            'scrollToComment': true,
          };
        }
        break;
      case 'follow':
        title = '新粉丝';
        content = '${wsContent['senderName'] ?? '有人'}关注了您';
        // 跳转到用户资料页
        navigationPath = '/profile/${wsContent['senderId']}';
        navigationParams = {'userId': wsContent['senderId']};
        break;
      default:
        title = '系统通知';
        content = wsContent['message']?.toString() ?? '您有新的通知';
        // 系统通知可能跳转到不同页面，根据具体类型决定
        navigationPath = wsContent['navigationPath'];
        navigationParams = wsContent['navigationParams'] as Map<String, dynamic>?;
    }

    return NotificationMessage(
      id: wsContent['messageId']?.toString() ?? DateTime.now().millisecondsSinceEpoch.toString(),
      type: notificationType,
      title: title,
      content: content,
      senderName: wsContent['senderName'],
      senderId: wsContent['senderId']?.toString(),
      targetId: wsContent['postId']?.toString() ?? wsContent['commentId']?.toString(),
      targetType: wsContent['targetType'],
      navigationPath: navigationPath,
      navigationParams: navigationParams,
      extraData: wsContent,
      timestamp: wsContent['timestamp'] != null 
          ? DateTime.fromMillisecondsSinceEpoch(wsContent['timestamp'])
          : DateTime.now(),
      isRead: false,
      isOffline: false,
    );
  }

  /// 创建副本并修改某些属性
  NotificationMessage copyWith({
    String? id,
    String? type,
    String? title,
    String? content,
    String? senderName,
    String? senderId,
    String? targetId,
    String? targetType,
    String? navigationPath,
    Map<String, dynamic>? navigationParams,
    Map<String, dynamic>? extraData,
    DateTime? timestamp,
    bool? isRead,
    bool? isOffline,
  }) {
    return NotificationMessage(
      id: id ?? this.id,
      type: type ?? this.type,
      title: title ?? this.title,
      content: content ?? this.content,
      senderName: senderName ?? this.senderName,
      senderId: senderId ?? this.senderId,
      targetId: targetId ?? this.targetId,
      targetType: targetType ?? this.targetType,
      navigationPath: navigationPath ?? this.navigationPath,
      navigationParams: navigationParams ?? this.navigationParams,
      extraData: extraData ?? this.extraData,
      timestamp: timestamp ?? this.timestamp,
      isRead: isRead ?? this.isRead,
      isOffline: isOffline ?? this.isOffline,
    );
  }

  /// 获取通知图标
  String getNotificationIcon() {
    switch (type) {
      case 'comment_reply':
      case 'post_reply':
        return '💬';
      case 'like':
        return '👍';
      case 'follow':
        return '👤';
      case 'system':
        return '📢';
      default:
        return '🔔';
    }
  }

  /// 获取通知颜色
  String getNotificationColor() {
    switch (type) {
      case 'comment_reply':
      case 'post_reply':
        return '#2196F3'; // 蓝色
      case 'like':
        return '#FF5722'; // 橙红色
      case 'follow':
        return '#4CAF50'; // 绿色
      case 'system':
        return '#9C27B0'; // 紫色
      default:
        return '#757575'; // 灰色
    }
  }
}